package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.dto.UserDto;
import com.bjsxt.feign.UserServiceFeign;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.WorkIssue;
import com.bjsxt.mapper.WorkIssueMapper;
import com.bjsxt.service.WorkIssueService;
import org.springframework.util.CollectionUtils;

@Service
public class WorkIssueServiceImpl extends ServiceImpl<WorkIssueMapper, WorkIssue> implements WorkIssueService{

    @Autowired
    UserServiceFeign userServiceFeign;

    @Override
    public Page<WorkIssue> findByPage(Page<WorkIssue> page, Integer status, String startTime, String endTime) {
      Page<WorkIssue> pageData = page(page, new LambdaQueryWrapper<WorkIssue>().eq(status!= null,WorkIssue::getStatus,status)
              .between(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime),WorkIssue::getCreated,startTime,endTime+" 23:59:59"  )   );

        List<WorkIssue> records = pageData.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return pageData;
        }
  //远程调用 member-service

        //1.id查找
        List<Long> userIds =   records.stream().map(WorkIssue::getUserId).collect(Collectors.toList());
      //2.远程调用
       // List<UserDto> basicUsers = userServiceFeign.getBasicUsers(userIds);
   //2.1 list - > mapper
//        if(CollectionUtils.isEmpty(basicUsers)){
//            return pageData;
//        }
//   Map<Long,UserDto> idMapUserDtos =      basicUsers.stream().collect(Collectors.toMap(userDto -> userDto.getId(), userDto -> userDto));
//
        Map<Long, UserDto> idMapUserDtos = userServiceFeign.getBasicUsers(userIds, null, null);
        records.forEach(workIssue -> {
            UserDto userDto = idMapUserDtos.get(workIssue.getUserId());
            workIssue.setUsername(userDto==null?"测试用户":userDto.getUsername());
            workIssue.setRealName(userDto==null?"测试用户":userDto.getRealName());
        });

        return pageData;

    }

    @Override
    public Page<WorkIssue> getIssueList(Page<WorkIssue> page,Long userIdStr) {
        return page(page,new LambdaQueryWrapper<WorkIssue>().eq(WorkIssue::getUserId,userIdStr)
             //   .eq(WorkIssue::getStatus,1)


        );
    }
}






















































